﻿using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LinqToOwin.UnitTests
{
  [TestClass]
  public class ResponderTests
  {
    [TestMethod]
    public void IsXml_Null()
    {
      var isXml = Responder.IsXml(null);

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_Empty()
    {
      var isXml = Responder.IsXml(string.Empty);

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_Whitespace()
    {
      var isXml = Responder.IsXml("       ");

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_Text()
    {
      var isXml = Responder.IsXml("This is not XML.");

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_Text_WithLeadingWhitespace()
    {
      var isXml = Responder.IsXml("    This is not XML.");

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_Comment()
    {
      var isXml = Responder.IsXml("<!--Comment-->");

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_Comment_WithLeadingWhitespace()
    {
      var isXml = Responder.IsXml("     <!--Comment-->");

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_DocType()
    {
      var isXml = Responder.IsXml("<!DOCTYPE xml >");

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_DocType_WithLeadingWhitespace()
    {
      var isXml = Responder.IsXml("     <!DOCTYPE xml >");

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_Html()
    {
      var isXml = Responder.IsXml("<html></html>");

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_Html_WithLeadingWhitespace()
    {
      var isXml = Responder.IsXml("     <html></html>");

      Assert.IsFalse(isXml);
    }

    [TestMethod]
    public void IsXml_XmlDeclaration_Invalid()
    {
      var isXml = Responder.IsXml(@"<?xml INVALID ?> INVALID-XML");

      Assert.IsTrue(isXml);
    }

    [TestMethod]
    public void IsXml_XmlDeclaration_Invalid_WithLeadingWhitespace()
    {
      var isXml = Responder.IsXml(@"      <?xml INVALID ?> INVALID-XML");

      Assert.IsTrue(isXml);
    }

    [TestMethod]
    public void IsXml_XmlDeclaration()
    {
      var isXml = Responder.IsXml(@"<?xml version=""1.0""?>");

      Assert.IsTrue(isXml);
    }

    [TestMethod]
    public void IsXml_XmlDeclaration_WithLeadingWhitespace()
    {
      var isXml = Responder.IsXml(@"      <?xml version=""1.0""?>");

      Assert.IsTrue(isXml);
    }

    [TestMethod]
    public void IsXml_Xml_Invalid()
    {
      var isXml = Responder.IsXml(@"<root INVALID> INVALID-XML");

      Assert.IsTrue(isXml);
    }

    [TestMethod]
    public void IsXml_Xml_Invalid_WithLeadingWhitespace()
    {
      var isXml = Responder.IsXml(@"      <root INVALID> INVALID-XML");

      Assert.IsTrue(isXml);
    }

    [TestMethod]
    public void IsXml_Xml()
    {
      var isXml = Responder.IsXml(@"<root> </root>");

      Assert.IsTrue(isXml);
    }

    [TestMethod]
    public void IsXml_Xml_WithLeadingWhitespace()
    {
      var isXml = Responder.IsXml(@"      <root> </root>");

      Assert.IsTrue(isXml);
    }

    [TestMethod]
    public void IsXml_XDocument()
    {
      var isXml = Responder.IsXml(new XDocument());

      Assert.IsTrue(isXml);
    }

    [TestMethod]
    public void IsXml_XElement()
    {
      var isXml = Responder.IsXml(new XElement("root"));

      Assert.IsTrue(isXml);
    }
  }
}